package lrscp.lib;

import java.util.HashMap;
import java.util.Map;

import lrscp.lib.utils.XmlUtils;

public class GlobalConfig {
    static final String CONFIG_FILE = "global.xml";
    static GlobalConfig cfg;

    static Object mLock = new Object();

    public static GlobalConfig getInstance() {
        synchronized (mLock) {
            if (cfg == null) {
                cfg = (GlobalConfig) XmlUtils.restore(CONFIG_FILE);
                if (cfg == null)
                    cfg = new GlobalConfig();
            }
        }
        return cfg;
    }

    private GlobalConfig() {}

    private Map<String, Object> configs = new HashMap<String, Object>();

    public synchronized Object get(String key, Object defaultValue) {
        if (configs.containsKey(key))
            return configs.get(key);
        else {
            set(key, defaultValue);
            return defaultValue;
        }
    }

    private synchronized void set(String key, Object config) {
        configs.put(key, config);
    }

    public void save() {
        XmlUtils.save(this, CONFIG_FILE);
    }
}
